//
// Created by xiaoma on 1/28/21.
//

#include "HiString.h"
#include <cstring>
#include <cassert>
#include <iostream>

void StringKlass::print(HiObject *object) {
    auto obj = (HiString *) object;
    assert(obj && obj->klass() == (Klass *) this);
    std::cout << obj->value() << std::endl;
}

HiBool *StringKlass::equal(HiObject *lhs, HiObject *rhs) {
    if (lhs->klass() != rhs->klass()) {
        return Universe::False;
    }
    auto l = (HiString *) lhs;
    auto r = (HiString *) rhs;
    assert(l && l->klass() == (Klass *) this);
    assert(r && r->klass() == (Klass *) this);

    return strcmp(l->value(), r->value()) == 0 ? Universe::True : Universe::False;
}

HiString::HiString(const char *str) {
    setKlass(StringKlass::getKlass());
    _len = strlen(str);
    _value = new char[_len];
    strcpy(_value, str);
}

HiString::HiString(const char *str, int length) {
    setKlass(StringKlass::getKlass());
    _len = length;
    _value = new char[_len];
    for (int i = 0; i < _len; ++i) {
        _value[i] = str[i];
    }
}
